package zoo.member.admin.pro.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import zoo.member.admin.pro.dao.AdminProductDao;
import zoo.member.admin.pro.domain.AdminProductCommand;
import zoo.member.admin.util.FileUtil;

@Controller
public class AdminProductDeleteController {
	
	@Autowired
	private AdminProductDao apDao;
	
	@ModelAttribute("adminProductCommand")
	public AdminProductCommand back(){
		return new AdminProductCommand();
	}
	
	@RequestMapping("/admin/prodelete.do")
	public String process(@RequestParam("p_code")int p_code
			){
		
		AdminProductCommand command = apDao.selectProBoard(p_code);
		
		apDao.deleteProBoard(p_code);
		
		System.out.println(p_code);
		
		//파일 삭제 여부 체크
		if(command.getP_listimage() != null){
			FileUtil.removeFile(command.getP_listimage());
		} else if(command.getP_previewimage1() != null){
			FileUtil.removeFile(command.getP_previewimage1());
		} else if(command.getP_previewimage2() != null){
			FileUtil.removeFile(command.getP_previewimage2());
		} else if(command.getP_previewimage3() != null){
			FileUtil.removeFile(command.getP_previewimage3());
		} else if(command.getP_productimage1() != null){
			FileUtil.removeFile(command.getP_productimage1());
		} else if(command.getP_productimage2() != null){
			FileUtil.removeFile(command.getP_productimage2());
		} else if(command.getP_productimage3() != null){
			FileUtil.removeFile(command.getP_productimage3());
		}

		return "redirect:/admin/prolist.do";
	}
}
